/*
 * Created by JFormDesigner on Wed Jul 03 16:12:09 CST 2024
 */

package scct.yzx.ui;

import scct.yzx.dao.ChatRecordDao;
import scct.yzx.model.ChatRecord;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.List;
import java.util.Objects;

/**
 * @author lenovo
 */
public class PanelChatRecord extends JPanel {
    DefaultTableModel Searchmodel = null;
    DefaultTableModel model;
    FormMain main;
    public PanelChatRecord(FormMain main) {
        this.main=main;
//        初始化组件
        initComponents();
//        初始化视图
        initView();
    }

    private void initView() {
        model = (DefaultTableModel) tabChat.getModel();
        model.addColumn("id");
        model.addColumn("会话id");
        model.addColumn("发送者");
        model.addColumn("接收者");
        model.addColumn("消息类型");
        model.addColumn("消息内容");
        model.addColumn("时间");
        getTable();


    }

    private void getTable() {
        model.setRowCount(0);
        ChatRecordDao chatRecordDao=new ChatRecordDao();
        List<ChatRecord> allChatRecord=chatRecordDao.getAllChatRecord();
        for (ChatRecord chatRecord:allChatRecord){
            model.addRow(new Object[]{chatRecord.getId(),chatRecord.getConverstion(),chatRecord.getSendername(),chatRecord.getReceivername(),chatRecord.getMsgtype(),chatRecord.getContent(),chatRecord.getSentime()});
        }
    }

    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        DlgChatRecordAdd dlgChatRecordAdd=new DlgChatRecordAdd(main);
        dlgChatRecordAdd.setModal(true);
        dlgChatRecordAdd.setVisible(true);
        getTable();
    }

    private void btnUpdate(ActionEvent e) {
        // TODO add your code here
        if (tabChat.getSelectedRowCount()==0){
            JOptionPane.showMessageDialog(this,"请选择要修改的聊天记录");
            return;
        }
        int index=tabChat.getSelectedRow();
        int id=Integer.parseInt(model.getValueAt(index,0).toString());
        DlgChatRecordUpdate dlgChatRecordUpdate=new DlgChatRecordUpdate(main,id);
        dlgChatRecordUpdate.setModal(true);
        dlgChatRecordUpdate.setVisible(true);
        getTable();
    }

    private void btnDelete(ActionEvent e) {
        // TODO add your code here
        if (tabChat.getSelectedRowCount()==0){
            JOptionPane.showMessageDialog(this,"请选择要删除的聊天记录");
            return;
        }
        int index=tabChat.getSelectedRow();
        int id=Integer.parseInt(model.getValueAt(index,0).toString());
        DlgChatRecordDelete dlgChatRecordDelete=new DlgChatRecordDelete(main,id);
        dlgChatRecordDelete.setModal(true);
        dlgChatRecordDelete.setVisible(true);
        getTable();
    }

    private void btnSearch(ActionEvent e) {
        // TODO add your code here
        System.out.println(e.getSource());
        tabChat.setModel(model);
        Searchmodel = new DefaultTableModel();
        Searchmodel.addColumn("id");
        Searchmodel.addColumn("会话id");
        Searchmodel.addColumn("发送者");
        Searchmodel.addColumn("接收者");
        Searchmodel.addColumn("消息类型");
        Searchmodel.addColumn("消息内容");
        Searchmodel.addColumn("时间");
        Searchmodel.setRowCount(0);
        String searchkey = txtSearch.getText();
        System.out.println(searchkey);
        String searchList = (String) comSearch.getSelectedItem();
        System.out.println(searchList);
        int row = tabChat.getRowCount();
        System.out.println("row:" + row);
        int column = tabChat.getColumnCount();
        System.out.println("column:" + column);
        for (int i = 0; i < row; i++) {
            if (tabChat.getRowCount() < row){
            }else {
                for (int j = 0; j < column; j++) {


                    if (Objects.equals(searchList, tabChat.getColumnName(j))) {
                        System.out.println("单元格的值：" + tabChat.getValueAt(i, j));
                        String cellValue = tabChat.getValueAt(i, j).toString().toLowerCase();
                        System.out.println("cellValue:" + cellValue);
                        if (cellValue.contains(searchkey)) {
                            Object[] rowDate = new Object[tabChat.getColumnCount()];
                            for (int k = 0; k < tabChat.getColumnCount(); k++) {
                                rowDate[k] = tabChat.getValueAt(i, k);
                                System.out.println(rowDate[k]);
                            }
                            Searchmodel.addRow(rowDate);
                            tabChat.setRowSelectionAllowed(true);
                            break;
                        }

                    }
                }
            }
        }
        tabChat.setModel(Searchmodel);
    }





    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - zhang
        panel1 = new JPanel();
        comSearch = new JComboBox<>();
        txtSearch = new JTextField();
        btnSearch = new JButton();
        btnAdd = new JButton();
        btnUpdate = new JButton();
        btnDelele = new JButton();
        scrollPane1 = new JScrollPane();
        tabChat = new JTable();

        //======== this ========
        setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.TitledBorder(new javax.
        swing.border.EmptyBorder(0,0,0,0), "JF\u006frmDesi\u0067ner Ev\u0061luatio\u006e",javax.swing.border
        .TitledBorder.CENTER,javax.swing.border.TitledBorder.BOTTOM,new Font("Dialo\u0067"
        , Font.BOLD,12), Color.red), getBorder
        ())); addPropertyChangeListener(new java.beans.PropertyChangeListener(){@Override public void propertyChange(java
        .beans.PropertyChangeEvent e){if("borde\u0072".equals(e.getPropertyName()))throw new RuntimeException
        ();}});
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //---- comSearch ----
            comSearch.setModel(new DefaultComboBoxModel<>(new String[] {
                "id",
                "\u4f1a\u8bddid",
                "\u53d1\u9001\u8005",
                "\u63a5\u6536\u8005",
                "\u6d88\u606f\u7c7b\u578b",
                "\u6d88\u606f\u5185\u5bb9",
                "\u65f6\u95f4"
            }));
            panel1.add(comSearch);
            comSearch.setBounds(new Rectangle(new Point(10, 5), comSearch.getPreferredSize()));
            panel1.add(txtSearch);
            txtSearch.setBounds(115, 5, 125, txtSearch.getPreferredSize().height);

            //---- btnSearch ----
            btnSearch.setText("\u641c\u7d22");
            btnSearch.addActionListener(e -> btnSearch(e));
            panel1.add(btnSearch);
            btnSearch.setBounds(new Rectangle(new Point(240, 5), btnSearch.getPreferredSize()));

            //---- btnAdd ----
            btnAdd.setText("\u65b0\u589e");
            btnAdd.addActionListener(e -> btnAdd(e));
            panel1.add(btnAdd);
            btnAdd.setBounds(new Rectangle(new Point(376, 5), btnAdd.getPreferredSize()));

            //---- btnUpdate ----
            btnUpdate.setText("\u4fee\u6539");
            btnUpdate.addActionListener(e -> btnUpdate(e));
            panel1.add(btnUpdate);
            btnUpdate.setBounds(new Rectangle(new Point(459, 5), btnUpdate.getPreferredSize()));

            //---- btnDelele ----
            btnDelele.setText("\u5220\u9664");
            btnDelele.addActionListener(e -> btnDelete(e));
            panel1.add(btnDelele);
            btnDelele.setBounds(new Rectangle(new Point(542, 5), btnDelele.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        add(panel1, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(tabChat);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - zhang
    private JPanel panel1;
    private JComboBox<String> comSearch;
    private JTextField txtSearch;
    private JButton btnSearch;
    private JButton btnAdd;
    private JButton btnUpdate;
    private JButton btnDelele;
    private JScrollPane scrollPane1;
    private JTable tabChat;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
